<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/27 14:10
 * Desc: 首页
 */
class Service_Api_Index extends Contract_Api_Index
{

    public function index_nav(array $params): array
    {
        /**
         * @var $client_no
         */
        extract($params);

        $mini_type = Arr::get($params, 'mini_type', '');
        $secret    = Arr::get($params, 'secret', '');
        $siteId    = Arr::get($params, 'siteId', '');

        // 检查小程序是否有自定义导航
        $nav = DB::select()
            ->from("uniapp_nav")
            ->where("m_isopen", "=", 1)
            ->order_by("m_displayorder", "ASC")
            ->execute()
            ->as_array();

        // 默认打开的权限
        $default_arr = array('destinationIndex', 'clubIndex','question');
        $newNav      = array();

        if (!empty($nav)) {

            // 获取小程序权限
            $auth = Model_Api_V2_System::get_xcx_auth($mini_type, $siteId, $secret);

            foreach ($nav as &$v) {
                // 检查是否需要在当前端显示
                if ($v['m_client'] != NULL) {
                    $m_client = explode(',', $v['m_client']);
                    if (!in_array($client_no, $m_client)) {
                        continue;
                    }
                }

                $v['m_ico'] = $v['m_ico'] ? $this->img_url($v['m_ico']) : '';
                if ($v['type'] == 3 || $v['type'] == 4) {
                    $newNav[] = $v;
                } else {
                    if ($v['m_mode'] != NULL) {
                        if (in_array($v['m_mode'], $default_arr) || in_array($v['m_mode'], $auth)) {
                            $newNav[] = $v;
                        }
                    } else {
                        $newNav[] = $v;
                    }
                }
            }
        }

        return array(
            'list' => $newNav
        );
    }

    public function foot_menu(array $params): array
    {
        /**
         * @var $client_no
         */
        extract($params);

        $mini_type = Arr::get($params, 'mini_type', '');
        $secret    = Arr::get($params, 'secret', '');
        $siteId    = Arr::get($params, 'siteId', '');

        // 检查小程序是否有自定义导航
        $nav = DB::select('*', DB::expr('IFNULL(`m_displayorder`, 9999) m_displayorder'))
            ->from("uniapp_footmenu")

            ->where_open()
            ->where('m_isopen','=',1)
            ->where_close()
            ->or_where_open()
            ->where('id','in',[1,2])
            ->or_where_close()

            ->order_by("m_displayorder", "ASC")
            ->limit(5)
            ->execute()
            ->as_array();

        // 默认打开的权限
        $default_arr = array('destinationIndex', 'clubIndex');
        $new_nav     = array();

        if (!empty($nav)) {

            // 获取小程序权限
            $auth = Model_Api_V2_System::get_xcx_auth($mini_type, $siteId, $secret);

            foreach ($nav as &$v) {
                // 检查是否需要在当前端显示
                if ($v['m_client'] != NULL) {
                    $m_client = explode(',', $v['m_client']);
                    if (!in_array($client_no, $m_client)) {
                        continue;
                    }
                }

                if ($v['id'] > 2) {
                    //除了系统自带的 选中 和未选中一样
                    $v['m_ico1']      = $this->img_url($v['m_ico1']);
                    $v['m_ico2']      = $v['m_ico1'];
                    $v['font_color2'] = $v['font_color1'];
                } else {
                    $v['m_ico1'] = $this->img_url($v['m_ico1']);
                    $v['m_ico2'] = $this->img_url($v['m_ico2']);
                }

                if ($v['type'] == 3 || $v['type'] == 4) {
                    $new_nav[] = $v;
                } else {
                    if ($v['m_mode'] != NULL) {
                        if (in_array($v['m_mode'], $default_arr) || in_array($v['m_mode'], $auth)) {
                            $new_nav[] = $v;
                        }
                    } else {
                        $new_nav[] = $v;
                    }
                }
            }

            // 将个人中心放到最后
            for ($i = 0; $i < count($new_nav); $i++) {
                if ($new_nav[$i]['id'] == 2) {
                    $tmp = $new_nav[$i];
                    unset($new_nav[$i]);
                }
            }

            if (isset($tmp)) {
                $new_nav[] = $tmp;
            }
        }

        return array(
            'list' => array_values($new_nav)
        );
    }

    public function hot_raiders(array $params): array
    {
        $row      = Arr::get($params, 'row', 3);
        $list     = Taglib_Article::query(array('flag' => 'order', 'row' => $row));
        $new_list = array();
        foreach ($list as $item) {
            $new_list[] = array(
                'litpic' => $this->img_url($item['litpic']),
                'title' => $item['title'],
                'id' => $item['id'],
                'shownum' => $item['shownum'],
                'commentnum' => $item['commentnum'],
                'addtime_str' => $item['addtime'] ? date("Y-m-d", $item['addtime']) : '',
                'summary' => $item['summary'],
                'author' => $item['author'],
            );
        }

        return array(
            'list' => $new_list
        );
    }

    public function hot_notes(array $params): array
    {
        $row = Arr::get($params, 'row', 3);

        $list     = Taglib_Notes::query(array('flag' => 'hot', 'row' => $row));
        $new_list = array();
        foreach ($list as $item) {
            $new_list[] = array(
                'litpic' => Model_Api_V2_System::uniapp_img($item['litpic']),
                'memberpic' => Model_Api_V2_Common::get_member_header_pic($item['memberpic']),
                'title' => $item['title'],
                'shownum' => $item['shownum'],
                'read_num' => $item['read_num'],
                'nickname' => $item['nickname'],
                'addtime' => $item['addtime'] ? date("Y-m-d", $item['addtime']) : '',
                'id' => $item['id'],
                'description' => strip_tags($item['content']),
            );
        }

        return array(
            'list' => $new_list
        );
    }

    public function hot_zt($params): array
    {
        $row     = Arr::get($params, 'row', 4);
        $page    = 1;
        $keyword = '';
        $params  = array(
            'sorttype' => 0
        );

        $res      = Model_Zt::search_result($params, $keyword, $page, $row);
        $new_list = array();
        foreach ($res['list'] as $v) {
            $new_list[] = array(
                'title' => $v['title'],
                'litpic' => $this->img_url($v['m_banner']),
                'addtime' => $v['addtime'] ? date("Y-m-d", $v['addtime']) : '',
                'shownum' => $v['shownum'] ?: 0,
                'id' => $v['id'],
            );
        }

        return array(
            'list' => $new_list
        );
    }

    public function hot_product_nav(array $params): array
    {
        $secret        = Arr::get($params, 'secret', '');
        $site_id       = Arr::get($params, 'siteId', '');
        $row           = Arr::get($params, 'row', 4);
        $open_type_ids = Arr::get($params, 'open_type_ids', '');

        $type_ids = Model_Api_V2_System::get_uniapp_product_typeid($site_id, $secret, $open_type_ids);

        $list = DB::select()
            ->from('m_nav')
            ->where('m_isopen', '=', '1')
            ->and_where('m_typeid', 'in', explode(',', $type_ids))
            ->and_where('m_typeid', 'not in', array(4, 101, 106))
            ->order_by('m_displayorder', 'asc')
            ->limit($row)
            ->execute()
            ->as_array();

        foreach ($list as &$v) {
            $v = array(
                'id' => $v['id'],
                'm_typeid' => $v['m_typeid'],
                'm_title' => $v['m_title'],
            );
        }

        return $list;
    }

    public function hot_product(array $params): array
    {
        $type_ids    = Arr::get($params, 'typeid');
        $type_id_arr = explode(',', $type_ids);
        $row         = Arr::get($params, 'row', 10);
        if (count($type_id_arr) > 1) {
            $data = array();
            foreach ($type_id_arr as $type_id) {
                $list   = Model_Api_V2_Index::get_hot_product($type_id, array('row' => $row,'platform_no'=>$params['platform_no'] ));
                $data[] = compact('type_id', 'list');
            }
        } else {
            $data = Model_Api_V2_Index::get_hot_product($type_id_arr[0], array('row' => $row,'platform_no'=>$params['platform_no']));
        }

        return $data;
    }

    public function notice(array $params): array
    {
        $type_id = Arr::get($params, 'typeid', '');
        $row     = Arr::get($params, 'row', 10);
        $flag    = Arr::get($params, 'flag', 'all');

        $params = array('row' => $row, 'flag' => $flag, 'offset' => 0, 'typeid' => $type_id, 'limit' => 0);
        return Model_Api_V2_Order::query($params);
    }

    public function is_show_distribute(array $params): bool
    {
        if (!St_Functions::is_normal_app_install('mobiledistribution')) {
            return false;
        }

        $app_type = Arr::get($params, 'app_type', '');
        switch ($app_type) {
            case 'h5':
                $varname = 'cfg_fenxiao_ishow_h5';
                break;
            case 'douyin':
                $varname = 'cfg_fenxiao_ishow_dy';
                break;
            case 'wx':
                $varname = 'cfg_fenxiao_ishow_wx';
                break;
            default:
                break;
        }

        if (!isset($varname)) {
            return false;
        }

        $data = Model_Fenxiao_Config::get_val($varname);

        return $data && $data['value'] == 1;
    }

    public function hot_wenlv_index(array $params): array
    {
        $secret        = Arr::get($params, 'secret', '');
        $site_id       = Arr::get($params, 'siteId', '');
        $row           = Arr::get($params, 'row', 4);
        $open_type_ids = Arr::get($params, 'open_type_ids', '');

        $type_ids     = Model_Api_V2_System::get_uniapp_product_typeid($site_id, $secret, $open_type_ids);
        $type_ids     = explode(',', $type_ids);
        $row          = $row ?: 10;
        $hot_type_ids = array();
        foreach ($type_ids as $v) {
            if ($v != 106) {
                $hot_type_ids[] = $v;
            }
        }

        $data = array();
        if (count($hot_type_ids) > 0) {
            $navs = Model_Api_V2_Index::get_m_nav_by_type_ids($hot_type_ids);
            // 查询产品的基础条件
            $params = array(
                'row' => $row,
                'image_w' => 340,
                'image_h' => 440,
            );

            $all_items = array();
            $all       = array();
            foreach ($navs as &$v) {
                $items = Model_Api_V2_Index::get_hot_product($v['m_typeid'], $params);
                if (count($items) > 0) {
                    $items       = Model_Api_V2_Index::format_wenlv_data($items, $v['m_typeid']);
                    $v['typeid'] = $v['m_typeid'];
                    $v['title']  = $v['m_title'];
                    unset($v['m_typeid']);
                    unset($v['m_title']);
                    $all_items  = array_merge($all_items, $items);
                    $v['items'] = $items;
                    $data[]     = $v;
                }
            }

            $hot_products = Model_Api_V2_Index::get_order_hot_product();
            if(count($hot_products)>0){
                $len = count($hot_products);
                if($len<20){
                    $unit_num = 20-$len;
                    $hot_products = array_merge($hot_products, array_slice($all_items,0,$unit_num));
                }
                $show_all = array('title' => '全部', 'typeid' => '', 'items' => $hot_products);
                array_unshift($data, $show_all);
            }else{
                //显示全部
                $total_num = count($all_items);
                if ($total_num > 0) {
                    if ($total_num > 20) {
                        // 从数组中随机取出20条数据
                        $all_keys = array_rand($all_items, 20);
                        foreach ($all_keys as $key) {
                            $all[] = $all_items[$key];
                        }
                    } else {
                        $all = $all_items;
                    }
                    // 打乱数组顺序
                    shuffle($all);
                    $show_all = array('title' => '全部', 'typeid' => '', 'items' => $all);
                    array_unshift($data, $show_all);
                }
            }
        }

        return $data;
    }

    public function wenlv_nav(array $params): array
    {

        $secret        = Arr::get($params, 'secret', '');
        $site_id       = Arr::get($params, 'siteId', '');
        $open_type_ids = Arr::get($params, 'open_type_ids', '');

        $type_ids     = Model_Api_V2_System::get_uniapp_product_typeid($site_id, $secret, $open_type_ids);
        $type_ids     = explode(',', $type_ids);
        $hot_type_ids = array();
        foreach ($type_ids as $v) {
            if ($v != 106) {
                $hot_type_ids[] = $v;
            }
        }
        $data = array();
        if (count($hot_type_ids) > 0) {

            $navs = Model_Api_V2_Index::get_m_nav_by_type_ids($hot_type_ids);

            // 查询产品的基础条件
            $params          = array(
                'row' => 1,
                'image_w' => '',
                'image_h' => '',
            );
            $product_nav     = array();
            $product_type_id = array();
            $article_nav     = array();
            $article_type_id = array();
            // 获取模块名
            $article_type_ids = Model_Api_V2_System::$article_type_ids;
            foreach ($navs as &$v) {
                $items = Model_Api_V2_Index::get_hot_product($v['m_typeid'], $params);
                if (count($items) > 0) {
                    if (in_array($v['m_typeid'], $article_type_ids)) {
                        // 文章类
                        if (empty($article_nav)) {
                            $article_nav = array(
                                'title' => '游玩攻略',
                                'type' => 'article',
                                'items' => array(
                                    array(
                                        'title' => '全部',
                                    )
                                )
                            );
                        }
                        $article_type_id[]      = $v['m_typeid'];
                        $article_nav['items'][] = array(
                            'title' => $v['m_title'],
                            'typeid' => $v['m_typeid'],
                        );
                    } else {
                        // 产品类
                        if (empty($product_nav)) {
                            $product_nav = array(
                                'title' => '发现趣玩',
                                'type' => 'product',
                                'items' => array(
                                    array(
                                        'title' => '全部',
                                    )
                                )
                            );
                        }
                        $product_type_id[]      = $v['m_typeid'];
                        $product_nav['items'][] = array(
                            'title' => $v['m_title'],
                            'typeid' => $v['m_typeid'],
                        );
                    }
                }
            }
            if (!empty($product_nav)) {
                $product_nav['items'][0]['typeid'] = implode(',', $product_type_id);
                $data[]                            = $product_nav;
            }
            if (!empty($article_nav)) {
                $article_nav['items'][0]['typeid'] = implode(',', $article_type_id);
                $data[]                            = $article_nav;
            }
        }

        return $data;
    }

    public function wenlv_list(array $params): array
    {
        $row         = Arr::get($params, 'row', 10);
        $page        = Arr::get($params, 'page', 1);
        $type_ids    = Arr::get($params, 'typeid');
        $type_id_arr = explode(',', $type_ids);

        $offset = St_Page::offset($page, $row);
        // 查询产品的基础条件
        $params = array(
            'row' => $row,
            'offset' => $offset,
            'image_w' => 340,
            'image_h' => 440,
        );
        $data   = array();
        if (count($type_id_arr) > 1) {
            $type_id_len      = count($type_id_arr);
            $params['row']    = ceil($row / $type_id_len);
            $params['offset'] = ($page - 1) * $params['row'];
            foreach ($type_id_arr as $type_id) {
                $items = Model_Api_V2_Index::get_hot_product($type_id, $params);
                if (count($items) > 0) {
                    $items = Model_Api_V2_Index::format_wenlv_data($items, $type_id);
                    $data  = array_merge($data, $items);
                }
            }
            shuffle($data);
        } else {
            $type_id = $type_id_arr[0];
            $data    = Model_Api_V2_Index::get_hot_product($type_id, $params);
            $data    = Model_Api_V2_Index::format_wenlv_data($data, $type_id);
        }

        return $data;
    }


}